//
//  EPMainTabsView.m
//  EnvironmentalProtection
//
//  Created by HCreater on 2018/9/28.
//  Copyright © 2018年 HCreater. All rights reserved.
//

#import "EPMainTabsView.h"

@interface EPMainTabsView()

@property (nonatomic, readwrite, strong) EPMainTabsViewModel* viewModel;

@end

@implementation EPMainTabsView

- (void)bindViewModel:(id)viewModel
{
    EPMainTabsViewModel* vm = viewModel;
    
    if (!self.viewModel)
    {
        self.viewModel = vm;
        
        [self _setup];
        
        [self bind];
    }
    else
    {
        self.viewModel.tabInfos = [vm.tabInfos copy];
    }
}

- (void)_setup
{
    @weakify(self)
    
}

- (void)bind
{
    @weakify(self)
    
    NSInteger tabCount = self.viewModel.tabInfos.count;
    CGFloat offset = 4.0f;
    CGFloat bgMinHeight = 2.0f;
//    CGSize tabSize = (CGSize) {(self.frame.size.width - offset*(tabCount + 1))/tabCount, self.frame.size.height - offset};
    
    [[RACObserve(self.viewModel, selectIndex) distinctUntilChanged]
     subscribeNext:^(id x) {
         
         @strongify(self)
         
         NSInteger curSIndex = [x integerValue];
         
         @weakify(self)
         [UIView animateWithDuration:0.1f animations:^{
             
             @strongify(self)
             
             NSInteger tabInfoCount = self.viewModel.tabInfos.count;
             for (NSInteger index=0; index<tabInfoCount; index++)
             {
                 UIView* curSubView = [self viewWithTag:10 + index];
                 
                 UILabel* label = [curSubView viewWithTag:101];
                 label.textColor = curSIndex == index ? self.viewModel.selectTextColor : self.viewModel.unselectTextColor;
             }
             
             UIView* curSelectSubView = [self viewWithTag:10 + curSIndex];
             UIView* blockView = [self viewWithTag:100];
             blockView.frame = (CGRect) {curSelectSubView.frame.origin.x + curSelectSubView.frame.size.width/3.0, CGRectGetMaxY(curSelectSubView.frame) - 4, curSelectSubView.frame.size.width/3.0, 4};
         }];
     }];
    
    [[[RACObserve(self.viewModel, tabInfos) distinctUntilChanged]
      filter:^BOOL(id value) {
          
          NSArray* curList = value;
          
          return curList.count > 0;
      }] subscribeNext:^(id x) {
          
          @strongify(self)
          
          [self removeAllSubviews];
          
          // Build Tab
          NSInteger tabCount = self.viewModel.tabInfos.count;
          
          CGFloat offset = 4.0f;
          CGSize tabSize = (CGSize) {(self.frame.size.width - offset*(tabCount + 1))/tabCount, self.frame.size.height - offset};
          CGRect firstTabRect = CGRectZero;
          
          for (NSInteger index=0; index<tabCount; index++)
          {
              NSDictionary* curTabInfo = self.viewModel.tabInfos[index];
              
              UIView* tabView = [[UIView alloc] initWithFrame:(CGRect) {offset + index*(tabSize.width + offset), 0, tabSize}];
              tabView.tag = 10+index;
              
              if (CGRectEqualToRect(firstTabRect, CGRectZero))
              {
                  firstTabRect = tabView.frame;
              }
              
              UILabel* label = [[UILabel alloc] initWithFrame:(CGRect) {0, 0, tabSize}];
              label.font = [UIFont systemFontOfSize:21.0/96.0*72.0 - 2];
              //             label.adjustsFontSizeToFitWidth = YES;
              label.text = curTabInfo[@"TabName"];
              label.textColor = index==self.viewModel.selectIndex?self.viewModel.selectTextColor:self.viewModel.unselectTextColor;
              label.textAlignment = NSTextAlignmentCenter;
              label.numberOfLines = 2;
              label.tag = 101;
              [tabView addSubview:label];
              
              UIButton* btn = [UIButton buttonWithType:UIButtonTypeCustom];
              btn.frame = label.frame;
              btn.tag = 102;
              [tabView addSubview:btn];
              
              CGFloat lineHeight = 1.0f;
              UIView* lineView = [[UIView alloc] initWithFrame:(CGRect) {offset, self.frame.size.height - lineHeight, self.frame.size.width - offset*2, lineHeight}];
              lineView.backgroundColor = UIColorHex(#bbbbbb);
              [tabView addSubview:lineView];
              
              [self addSubview:tabView];
          }
          
          // Build Block
          UIView* blockView = [[UIView alloc] initWithFrame:(CGRect) {firstTabRect.origin.x + firstTabRect.size.width/3.0, CGRectGetMaxY(firstTabRect) - 4, firstTabRect.size.width/3.0, 4}];
          blockView.backgroundColor = Color_MainGreen;
          blockView.tag = 100;
          [self addSubview:blockView];
      }];
    
    NSInteger tabInfoCount = self.viewModel.tabInfos.count;
    for (NSInteger index=0; index<tabInfoCount; index++)
    {
        UIView* curSubView = [self viewWithTag:10 + index];
        
        UIButton* curBtn = [curSubView viewWithTag:102];
        
        [[curBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            
            @strongify(self)
            
            [self.viewModel.didClickTab sendNext:@(index)];
            
            self.viewModel.selectIndex = index;
        }];
    }
}

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end

